package cn.sjtu.netty.bytebuffer;

import cn.sjtu.netty.util.ByteBufferUtil;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author syo 2319615164@qq.com
 * @description 写入ByteBuffer
 * @date 2024/6/21
 * @Copyright syo
 */
public class TestWriteByteBuffer {
    public static void main(String[] args) {
        // 1. 创建时就写入一定的数据
        byte[] bytes = new byte[]{0x61, 0x62, 0x63, 0x64, 0x65};
        ByteBuffer buffer1 = ByteBuffer.wrap(bytes);
        ByteBufferUtil.debugAll(buffer1);

        // 2. 从channel读往buffer写
        ByteBuffer buffer2 = ByteBuffer.allocate(1024);
        try (FileChannel channel = new RandomAccessFile("test.txt", "r").getChannel()) {
            channel.read(buffer2);
        } catch (IOException e) {
        }

        // 3. 直接put
        ByteBuffer buffer3 = ByteBuffer.allocate(1024);
        buffer3.put((byte) 0x61);
    }
}
